﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EMS20.UI.Admin.Cate
{
    using EMS20.Help;
    using EMS20.BusinessLogicLayer;
    using EMS20.Common;
    using EMS20.Entity;

    /// <summary>
    /// ProcessAjax 的摘要说明
    /// </summary>
    public class ProcessAjax : WebBaseHandler
    {

        public override void SubSubProcessRequest()
        {
            //获得请求操作
            string type = Request.QueryString["type"];

            //判断
            switch (type)
            {
                case "Del":
                    Delete();
                    break;
                case "GetEntity":
                    GetEntity();
                    break;
                case "Edit":
                    Edit();
                    break;
                case "Add":
                    Add();
                    break;
                default:
                    break;
            }
        }

        private void GetEntity()
        {
            string id = Request.QueryString["id"];
            if (string.IsNullOrEmpty(id) || !Kits.IsInt(id))
            {
                base.WriteFail("参数不合法");
                return;
            }

            try
            {
                CategoryEntity entity = Category_BLLSub.Get_CategoryEntity(int.Parse(id));

                base.WriteSuccess(entity);
            }
            catch (Exception ex)
            {
                base.WriteFail(ex.Message);
            }
        }

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        private void Delete()
        {
            string id = Request.QueryString["id"];
            if (string.IsNullOrEmpty(id) || !Kits.IsInt(id))
            {
                base.WriteFail("参数不合法");
                return;
            }

            try
            {
                int succes = Category_BLLSub.Create_CategoryDelete(int.Parse(id));

                //判断
                if (succes > 0)
                {
                    base.WriteSuccess("删除成功");
                }
                else
                {
                    base.WriteFail("删除失败");
                }
            }
            catch (Exception)
            {
                base.WriteFail("操作有误");
            }
        }
        #endregion

        #region 新增
        /// <summary>
        /// 新增
        /// </summary>
        private void Add()
        {
            string name = Request.Form["name"];
            string title = Request.Form["title"];
            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(title))
            {
                base.WriteFail("参数不合法");
                return;
            }

            CategoryEntity entity = new CategoryEntity()
            {
                c_title = title,
                parent_id = 0,
                c_type = int.Parse(name)
            };
            try
            {
                int success = Category_BLLSub.Create_CategoryInsert(entity);

                if (success > 0)
                {
                    base.WriteSuccess("新增成功");
                }
                else
                {
                    base.WriteFail("新增失败");
                }
            }
            catch (Exception ex)
            {

                base.WriteFail(ex.Message);
            }

        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改
        /// </summary>
        private void Edit()
        {
            string id = Request.Form["id"];
            string name = Request.Form["name"];
            string title = Request.Form["title"];
            if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(name) || string.IsNullOrEmpty(title) || !Kits.IsInt(id))
            {
                base.WriteFail("参数不合法");
                return;
            }

            CategoryEntity model = new CategoryEntity()
            {
                c_id = int.Parse(id),
                c_title = title,
                c_type = int.Parse(name),
                parent_id = 0
            };

            try
            {
                Category_BLLSub.Create_CategoryUpdate(model);
                base.WriteSuccess("修改成功");
            }
            catch (Exception ex)
            {

                base.WriteFail(ex.Message);
            }
        }
        #endregion

    }
}